<include file="header" />

<body>
<div class="wraper">
	<div class="head clearfix">
		<a href="javascript:history.go(-1);" class="iconfont icon-fanhui72 pull-left"></a>
		<span class="head-name pull-left">填写个人信息</span>
		<a href="{BEESCRM::U('base/bonsai/index', array('wechat_id'=>$wechat_id, 'origin'=>$origin))}" class="iconfont icon-home pull-left text-right"></a>
	</div>
	
	<if condition="$provider neq 1">
		<if condition="$data">
			<volist name="data" id="vo">
				<div class="tb-mode white-block">
					<div class="tb-cell info-left">
						<div class="image-box">
							<img src="{BEESCRM:$vo.goods.goods_image}"/>
						</div>
					</div>
					<div class="tb-cell line-h">
						<p class="text-green">{BEESCRM:$vo.goods.goods_name}</p>
						<if condition="$vo['type'] eq 2">
								<p class="sub-contain">会场布置: {BEESCRM:$vo.goods.meetingplace_price} 元/盆/场</p>
						<elseif condition="$vo['type'] eq 3"/>
								<p class="sub-contain">长期摆放: {BEESCRM:$vo.goods.long_price} 元/盆/天</p>
						</if>
						<p class="sub-contain">预约数量：{BEESCRM:$vo.goods_num}盆</p>
						<p class="sub-contain">小计：<span class="text-red">￥{BEESCRM:$vo.cart_price}元</span></p>
					</div>
				</div>
				<input name="cartId" value="{BEESCRM:$vo.cart_id}" type="hidden" class="cartId">
			</volist>
		</if>

		<input type="hidden" value="{BEESCRM:$goodsPrice}" id="goodsPrice"/>
		<p class="white-block text-right">预计总费用 <span class="text-red">￥<span id="goodsprice" data-goodsprice="{BEESCRM:$goodsPrice}">{BEESCRM:$goodsPrice}</span>元</span></p>
	</if>

	<p class="block-space">请填写您的真实信息</p>
	<div class="form-box">
		<p class="form-list"><input placeholder="请输入联系人姓名" id="username" value="{BEESCRM:$order.username}"/></p>
		<p class="form-list"><input placeholder="请输入联系方式" id="mobile" value="{BEESCRM:$order.mobile}"/></p>
		<p class="form-list">
			<select id="college">
				<option value="">请选择所属学院/部门</option>
				<option  value="1" selected>农林大学</option>
			</select>
			<span class="triangle"></span>
		</p>
		<!-- <p class="form-list">
			<select>
				<option>请选择摆放地点</option>
			</select>
			<span class="triangle"></span>
		</p> -->
		<p class="form-list"><input placeholder="请填写摆放部门" id="department" value="{BEESCRM:$order.department}"/></p>
		<p class="form-list"><input placeholder="请填写摆放地点" id="address" value="{BEESCRM:$order.address}"/></p>
		<p class="form-list"><input id="beginTime" placeholder="请选择开始摆放时间" /></p>
		<p class="form-list"><input id="endTime" placeholder="请选择结束摆放时间" /></p>
		<if condition="$goodstypes neq 2">
			<p class="form-list"><input type="number" placeholder="请填写使用时长,单位/天。如 1" id="uselong" /></p>
		<else/>
			<p class="form-list"><input type="hidden" placeholder="请填写使用时长,单位/天。如 1" id="uselong" value="1"/></p>
		</if>
		<!-- <p class="form-list">
			<select>
				<option>请选择要求完成时间</option>
			</select>
			<span class="triangle"></span>
		</p> -->
		<p class="form-list"><textarea placeholder="备注" id="remark"></textarea></p>
	</div>
	<div id="datePlugin"></div>
	<div class="btn-box text-center">
		<a href="javascript:;" class="btn btn-red" id="confirmSubmit">确认提交</a>
		<a href="javascript:history.go(-1);" class="btn btn-blue">取消预约</a>
	</div>
	<div class="pop-box" id="pop-box">
		<div class="main text-center">
			<p class="iconfont icon-shenheshibai"></p>
			<p class="line-h">预约无需支付费用</p>
			<p class="line-h">预约成功会有相关人员与您联系</p>
			<p class="line-h">请确认是否预约?</p>
			<div class="margin-t">
				<a href="javascript:;" class="btn btn-red" id="yesSubmit">确认提交</a>
				<a href="javascript:;" class="btn btn-blue cancel">取消</a>
			</div>
		</div>
	</div>
</div>

<include file="commonJs"/>
<script>
	var nav_h = $('.nav').height();
	$('.wraper').css('paddingBottom',nav_h+8);
	var img_h = $('.image-box img').height();
	$('.image-box img').css('marginTop',-img_h/2);

	<if condition="$goodstypes eq 2">
		$('#beginTime').date({theme:"datetime"});
		$('#endTime').date({theme:"datetime"});
	<elseif condition="$goodstypes eq 3"/>
		$('#beginTime').date();
		$('#endTime').date();

		$(document).on('click', '#uselong', function () {
			var strStartTime = $("#beginTime").val();
			var endTime = $("#endTime").val();

			if (strStartTime == "" || endTime == "") {
				$.alert('error', '请先选择开始和结束时间！');return ;
			}else {
				var startNum = parseInt(strStartTime.replace(/-/g, ''), 10);
				var endNum = parseInt(endTime.replace(/-/g, ''), 10);
				if (startNum > endNum) {
					$.alert('error', '结束时间不能在开始时间之前！');return ;
				}
				else {
					$("#uselong").val(diffDays(strStartTime, endTime));  //调用/计算两个日期天数差的函数，通用
					var goodsprice = $('#goodsPrice').val();
						//parseFloat($('#goodsPrice').html());
					var day = $("#uselong").val();
					var cost = (goodsprice*100)*day/100;
					cost = cost.toFixed(2);
					$('#goodsprice').html(cost);
				}
			}

//			function DateDiff(sDate1, sDate2) {  //sDate1和sDate2是yyyy-MM-dd格式
//				var aDate, oDate1, oDate2, iDays;
//				aDate = sDate1.split("-");
//				oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);  //转换为yyyy-MM-dd格式
//				aDate = sDate2.split("-");
//				oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
//				iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)+1; //把相差的毫秒数转换为天数
//
//				return iDays;  //返回相差天数
//			}
			function diffDays(s1,s2)//计算相差的天数
			{
				s1 = s1.replace(/-/g, "/");
				s2 = s2.replace(/-/g, "/");
				s1 = new Date(s1);
				s2 = new Date(s2);

				var days= s2.getTime() - s1.getTime();
				var time = parseInt(days / (1000 * 60 * 60 * 24))+1;

				return time;
			}
		});
		
	</if>
	
	

	/* $(document).on('click', '#uselong', function () {
		var strStartTime = $("#beginTime").val();
		var endTime = $("#endTime").val();

		if (strStartTime == "" || endTime == "") {
			$.alert('error', '请先选择开始和结束时间！');return ;
		}
		else {
			var startNum = parseInt(strStartTime.replace(/-/g, ''), 10);
			var endNum = parseInt(endTime.replace(/-/g, ''), 10);
			if (startNum > endNum) {
				$.alert('error', '结束时间不能在开始时间之前！');return ;
			}
			else {
				$("#uselong").val(DateDiff(strStartTime, endTime));  //调用/计算两个日期天数差的函数，通用
				var goodsprice = parseInt($('#goodsprice').html());
				var day = $("#uselong").val();
				$('#goodsprice').html(goodsprice*day);
			}
		}

		function DateDiff(sDate1, sDate2) {  //sDate1和sDate2是yyyy-MM-dd格式
			var aDate, oDate1, oDate2, iDays;
			aDate = sDate1.split("-");
			oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);  //转换为yyyy-MM-dd格式
			aDate = sDate2.split("-");
			oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
			iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)+1; //把相差的毫秒数转换为天数

			return iDays;  //返回相差天数
		}
	}); */
	
	//修改天数
	$(document).on('change', '#uselong', function () {
		var uselongs = $('#uselong').val();
		console.log(uselongs);
		var numReg = /^\+?[1-9][0-9]*$/;
		if (!numReg.test(uselongs)) {
			console.log('1s');
			$('#uselong').val('1');
		}
		
		var jsuselong = $('#uselong').val();
		var goodsprice = $('#goodsprice').data('goodsprice');
		var total = 0;
       	total = Number(goodsprice) * Number(jsuselong);
        $('#goodsprice').text(total.toFixed(2));
	});

	 $('#confirmSubmit').click(function(){
		 var username,mobile,college,collegeName,address,beginTime,endTime,uselong,remark,department;
			username = $('#username').val();
			mobile = $('#mobile').val();
			college = $('#college').val();
			collegeName = $('#college option:selected').text();
			address = $('#address').val();
			beginTime = $('#beginTime').val();
			endTime = $('#endTime').val();
			uselong = $('#uselong').val();
			remark = $('#remark').val();
			department = $('#department').val();
			
			<if condition="$provider neq 1">
				var cartId = [];
				$('.cartId').each(function(){
					cartId.push($(this).val());
				})
			</if>
			
			var numReg = /^\+?[1-9][0-9]*$/;
			var myreg = /^0?1[3|4|5|8][0-9]\d{8}$/;
			if(!username){
				$.alert('error', '请填写联系人姓名');return ;
			}else if(!myreg.test(mobile)){
		        $.alert('error', '请输入正确的手机号');return ;
			}else if(!college){
				$.alert('error', '请选择所属院校');return ;
			}else if(!department){
				$.alert('error', '请填写摆放部门');return ;
			}else if(!address){
				$.alert('error', '请填写摆放地点');return ;
			}else if(!beginTime || !endTime){
	            $.alert('error', '请选择摆放时间');return ;
	        }else if(!checkEndTime(beginTime, endTime)){	
	            $.alert('error', '开始摆放时间不能大于结束时间');return ;
	        }
			else if(!numReg.test(uselong) || isNaN(uselong)){
	        	$.alert('error', '使用时长必须是整数，默认单位/天');return ;
	        }
	        
			$('#pop-box').fadeIn(300);
	}); 
	
	$('#pop-box').click(function(){
		$('#pop-box').fadeOut(300);
	});
	
	$('#yesSubmit').click(function(){
		var username,mobile,college,collegeName,address,beginTime,endTime,uselong,remark,department;
		username = $('#username').val();
		mobile = $('#mobile').val();
		college = $('#college').val();
		collegeName = $('#college option:selected').text();
		address = $('#address').val();
		beginTime = $('#beginTime').val();
		endTime = $('#endTime').val();
		uselong = $('#uselong').val();
		remark = $('#remark').val();
		department = $('#department').val();
		
		<if condition="$provider neq 1">
			var cartId = [];
			$('.cartId').each(function(){
				cartId.push($(this).val());
			});
		</if>
		
		var numReg = /^\+?[1-9][0-9]*$/;
		var myreg = /^0?1[3|4|5|8][0-9]\d{8}$/;
		
		if(!username){
			$.alert('error', '请填写联系人姓名');return ;
		}else if(!myreg.test(mobile)){
	        $.alert('error', '请输入正确的手机号');return ;
		}else if(!college){
			$.alert('error', '请选择所属院校');return ;
		}else if(!department){
			$.alert('error', '请填写摆放部门');return ;
		}else if(!address){
			$.alert('error', '请填写摆放地点');return ;
		}else if(!beginTime || !endTime){
            $.alert('error', '请选择摆放时间');return ;
        }else if(!checkEndTime(beginTime, endTime)){	
            $.alert('error', '开始摆放时间不能大于结束时间');return ;
        }else if(!numReg.test(uselong) || isNaN(uselong)){
        	$.alert('error', '使用时长必须是整数，默认单位/天');return ;
        }
        
		$('#pop-box').fadeIn(300);
		
		var data = {}, json = {};
		data.username = username;
		data.mobile = mobile;
		data.college = collegeName;
		data.department = department;
		data.address = address;
		data.beginTime = beginTime;
		data.endTime = endTime;
		data.uselong = uselong;
		data.remark = remark;
		<if condition="$provider neq 1">
			data.cartId = cartId;
			data.solution = 0;
			data.provider = 0;
		<else/>
			data.solution = 1;
			data.provider = 1;
		</if>
		json.method = 'post';
		json.type = 'json';
		json.url = "{BEESCRM::U('base/bonsai/createOrder', array('wechat_id'=>$wechat_id, 'origin' => $origin))}";
		json.data = data;
		console.log(json);
		$.getAjax(json, true, redirect, '');		
	});
	
	function redirect(data){
		location.href = data.url;
	}
	
	//关闭弹窗
	$(document).on('click', '.cancel', function(){
		$('#pop-box').fadeOut(300);
	});
	
	//时间验证   yyyy-MM-dd HH:mm:ss
	function checkEndTime(beginTime ,endTime){  
	    var start = new Date(beginTime.replace("-", "/").replace("-", "/"));  
	    var end = new Date(endTime.replace("-", "/").replace("-", "/"));  
	    if(end < start){  
	        return false;  
	    }  
	    return true;  
	} 
	
</script>
</body>
</html>
